$(function(){
	var addUrl = '/index/add/clock';
	//上班打卡事件
	$('.toWork').on('tap',function(){
		var _this = this;
		punchTheClock(_this,'toWork');
	})
	//下班打卡事件
	$('.offWork').on('tap',function(){
		var _this = this;
		 punchTheClock(_this,'offWork');
	});
	function punchTheClock(_this,type){
		var workShift = $(".toWork").attr('time');
		var closingTime = $(".offWork").attr('time');
		var workarray = workShift.split(':');
		var closarray = closingTime.split(':');
        workShift = workarray[0]*3600+workarray[1]*60;
        closingTime = closarray[0]*3600+closarray[1]*60;
		//console.log(workarray[0]);
		// console.log(closingTime);
        var myDate = new Date();
		var title = '';
		var time = myDate.getHours()+':'+(myDate.getMinutes()>10?myDate.getMinutes():'0'+myDate.getMinutes());
		var mold = '';
		var theClass = $(_this).attr('class');
		if(type == 'toWork'){
			title = '确定上班打卡？';
			time = time;
			mold = (myDate.getHours()*3600+myDate.getMinutes()*60) <= workShift ? '正常上班' : '上班迟到';
		}else if(type == 'offWork'){
			title = '确定下班打卡？';
			time = time;
			mold = (myDate.getHours()*3600+myDate.getMinutes()*60) >= closingTime ? '正常下班' : '下班早退';
		}
		//判断有没有连接wifi
		if(theClass == type){
			//没有连接wifi
			layer.open({
			    content: '未连接WI-FI!'
			    ,skin: 'msg'
			    ,time: 2 //2秒后自动关闭
			});
		}else{
			//已连接wifi
			layer.open({
				shadeClose: false,
			    title: [
			      title,
			      'background-color: #4990E2; color:#fff;'
			    ]
			    ,content: '<div class="data-cont">'+
			    			 '<p>打卡时间：<span>'+time+'</span></p>'+
			    		     '<p>打卡类型：<span>'+mold+'</span></p>'+
			    		     '<div class="anniu">'+
				    		     '<a class="cancel mui-pull-left" href="javascript:;">取消打卡</a>'+
				    		     '<span class="anniu-line"></span>'+
				    		     '<a class="confirm mui-pull-right" href="javascript:;">确定打卡</a>'+
			    		     '</div>'+
			    		  '</div>'
			});
			$('.cancel').on('tap',function(){
			  	$(".layui-m-layer0").css({opacity:'0'});
				setTimeout(function(){
					var index = $('.layui-m-layer0').attr('index');
					layer.close(index);
				},500);
			})
			$('.confirm').on('tap',function(){
				console.log(type);
				console.log(time);
				$.post(addUrl,{type:type},function (data) {
					console.log(data);
					if(data.code==200){
                        cardSuccess(type,time);
					}else{
                        var index = $('.layui-m-layer0').attr('index');
                        layer.close(index);
						mui.alert(data.msg);
					}
                })

			})
		}
	}
	function cardSuccess(type,time){
		var encourage = '';
		var backColor = '';
		var borderColor = '';
		var form = '';
		if(type == 'toWork'){
			encourage = '一日之计在于晨！';
			backColor = '#4990E2';
			borderColor = '#60A7F9';
			form = '上班';
		}else if(type == 'offWork'){
			encourage = '下班休息，为明天而奋斗！';
			backColor = '#EF3684';
			borderColor = '#F662A0';
			form = '下班';
		}
		layer.open({
			shadeClose: false,
		    content: '<div class="punchOut" style="background:'+backColor+'">'+
		    			'<h3><img src="/static/images/dingwei.png"/>打卡成功</h3>'+
		    			'<div class="punchOut-time"><p class="punchOut-form">'+form+'</p><div class="punchOut-time-cont"><span>'+time+'</span></div></div>'+
		    		 	'<p class="punchOut-date">2018年6月6日 星期三</p>'+
		    		 	'<p class="punchOut-encourage">'+encourage+'</p>'+
		    		 	'<div class="punchOut-btn" style="border-top:1px solid '+borderColor+'"><a class="btn" href="javascript:;">我知道了</a></div>'+	
		    		 '</div>'
		});
		$('.btn').on('tap',function(){
			$(".layui-m-layer0").css({opacity:'0'});
			setTimeout(function(){
				var index = $('.layui-m-layer0').attr('index');
				layer.close(index);
			},500);
		})
	}
})
